package net.pinnoocle.playweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.WechatUserInfo;
import net.pinnoocle.playdao.entity.admin.InformDetailVo;
import net.pinnoocle.playdao.entity.admin.InformListPageVo;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.shopPc.*;
import net.pinnoocle.playdao.entity.web.banners.BannerListPageVo;
import net.pinnoocle.playdao.entity.web.group.GroupListPageVo;
import net.pinnoocle.playdao.entity.web.group.GroupListSearchParams;
import net.pinnoocle.playdao.entity.web.user.HomePageVo;
import net.pinnoocle.playdao.entity.web.user.LoginResultVo;
import net.pinnoocle.playservice.service.CommonService;
import net.pinnoocle.playservice.service.PayService;
import net.pinnoocle.playservice.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ye
 * @Description
 * @create 2021-10-25 17:34
 */
@RestController
@RequestMapping("/common")
@Api(tags = "公共操作(免登陆)")
public class CommonController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    CommonService commonService;
    @Autowired
    PayService payService;
    @Autowired
    InformServiceImpl informService;
    @Autowired
    ScriptServiceImpl scriptService;
    @Autowired
    GroupServiceImpl groupService;
    @Autowired
    BannersServiceImpl bannersService;
    @Autowired
    WxServiceImpl wxService;

    @PostMapping("userLogin")
    @ApiOperation("小程序用户授权")
    public BaseResult<LoginResultVo> userLogin(@RequestBody WechatUserInfo params){
        LoginResultVo loginResultVo = userService.userLogin(params);
        return new BaseResult<LoginResultVo>().ok(loginResultVo);
    }

    @GetMapping("homePage")
    @ApiOperation("首页")
    public BaseResult<HomePageVo> homePage(){
        return new BaseResult<HomePageVo>().ok(userService.homePage());
    }

    @GetMapping("scriptList")
    @ApiOperation("剧本库&筛选")
    public BaseResult<PageModel<ScriptListVo>> scriptList(ScriptListSearchParams params){
        params.setIsDisable(0);
        return new BaseResult<PageModel<ScriptListVo>>().ok(scriptService.scriptList(params));
    }

    @GetMapping("scriptDetail")
    @ApiOperation("剧本详情")
    public BaseResult<ScriptDetailVo> scriptDetail(@RequestParam("scriptId") String scriptId){
        return new BaseResult<ScriptDetailVo>().ok(scriptService.scriptDetail(scriptId));
    }

    @GetMapping("cateList")
    @ApiOperation("剧本分类列表")
    public BaseResult<List<ScriptCateListVo>> cateList(){
        return new BaseResult<List<ScriptCateListVo>>().ok(scriptService.cateList());
    }

    @GetMapping("labelList")
    @ApiOperation("剧本标签列表")
    public BaseResult<List<ScriptLabelListVo>> labelList(){
        return new BaseResult<List<ScriptLabelListVo>>().ok(scriptService.labelList());
    }

    @GetMapping("levelList")
    @ApiOperation("剧本难易级别列表")
    public BaseResult<List<ScriptLevelListVo>> levelList(){
        return new BaseResult<List<ScriptLevelListVo>>().ok(scriptService.levelList());
    }

    @GetMapping("groupList")
    @ApiOperation("未成局列表")
    public BaseResult<PageModel<GroupListPageVo>> groupList(GroupListSearchParams params){
        return new BaseResult<PageModel<GroupListPageVo>>().ok(groupService.groupList(params));
    }

    @GetMapping("informList")
    @ApiOperation("通知列表")
    public BaseResult<PageModel<InformListPageVo>> informList(){
        return new BaseResult<PageModel<InformListPageVo>>().ok(userService.informListWithWeb());
    }

    @GetMapping("informDetail")
    @ApiOperation("通知详情")
    public BaseResult<InformDetailVo> informDetail(@RequestParam("informId") String informId){
        return new BaseResult<InformDetailVo>().ok(informService.informDetail(informId));
    }

    /**
     * 定金订单异步通知
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("appointOrderNotify")
    public BaseResult<String> orderQuery(HttpServletRequest request, HttpServletResponse response){
        return new BaseResult<String>().ok(payService.payNotify(request, response, 1));
    }

    /**
     * 剧本支付订单异步通知
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("orderNotify")
    public BaseResult<String> orderNotify(HttpServletRequest request, HttpServletResponse response){
        return new BaseResult<String>().ok(payService.payNotify(request, response, 2));
    }

    @PostMapping("/uploadFile")
    @ApiOperation("文件上传")
    public BaseResult<String> uploadFiles(@RequestParam("file") MultipartFile file){
        return new BaseResult<String>().ok(commonService.uploadFile(file));
    }

    @GetMapping("bannerList")
    @ApiOperation("横幅图集")
    public BaseResult<List<BannerListPageVo>> bannerList(){
        return new BaseResult<List<BannerListPageVo>>().ok(bannersService.bannerList());
    }

    @GetMapping("startPushTicket")
    @ApiOperation("启动推送服务")
    public String startPushTicket(){
        return wxService.startPushTicket();
    }

    /**
     * 授权事件接收URL(推送令牌|快速注册小程序结果通知)
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("acceptOauth")
    public String acceptOauth(HttpServletRequest request, HttpServletResponse response){
        return wxService.acceptOauth(request, response);
    }

    /**
     * 消息与事件接收URL（代码审核结果通知）
     * @param appId
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("acceptMsg/{APPID}/callback")
    public String acceptMsg(@PathVariable("APPID") String appId ,HttpServletRequest request, HttpServletResponse response){
        return wxService.acceptMsg(request,response,appId);
    }

    /**
     * 授权回调url（扫码授权通知地址）
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("authRedirectUri")
    public String authRedirectUri(HttpServletRequest request, HttpServletResponse response){
        return wxService.authRedirectUri(request, response);
    }
}
